package hotel.action;

import hotel.data.MD5Creator;
import hotel.form.LoginForm;
import hotel.model.IUser;
import hotel.service.ServiceManager;
import hotel.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
 * This class check username and password and, if they compare with username and password in DB, create session
 * @author Alex
 *
 */
public class LoginAction extends Action 
{
	private static final Logger logger = Logger.getLogger(LoginAction.class);
	private static final String LOGIN_ERROR = "Error in LoginAction:";
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response) throws Exception 
	 {
		 String mapper = ForwardActionTo.ERROR;
		 try
		 {
		     LoginForm loginForm = (LoginForm)form;
		     HttpSession session = request.getSession(true);
		     ActionMessages errors = new ActionMessages();
		     IUser user = null;
			 UserService userService = null;
	
			 if (loginForm != null) 
			 {
				userService = ServiceManager.getDefault().getUserService();
	
				user = userService.getUserByUsername(loginForm.getUsername());
				if (loginForm.getUsername() == null )
				{
					errors.add("username", new ActionMessage("error.username"));
					saveMessages(request,errors);
				}
				else
					if ( loginForm.getUsername().equals("") )
			         {
			         	 errors.add("username", new ActionMessage("error.username"));
			         	saveMessages(request,errors);
			         } 
					 else
						 if ( user != null )
							 if (loginForm.getPassword() == null)
							 {
								 errors.add("password", new ActionMessage("error.password"));
								 saveMessages(request,errors);
							 }
							 else
								 if ( loginForm.getPassword().equals("") )
						         {
						        	 errors.add("password", new ActionMessage("error.password"));
						        	 saveMessages(request,errors);
						         }
								 else
									 if ( MD5Creator.MD5(loginForm.getPassword()).equals(user.getPassword()))
									 {
										 mapper = ForwardActionTo.SUCCESS;
										 session.setAttribute("user", user);
									 }
									 else
									 {
										 errors.add("wrongPassword", new ActionMessage("error.wrongpassword",loginForm.getUsername()));
										 saveMessages(request,errors);
									 }
						 else
						 {
							 errors.add("invalidUserName", new ActionMessage("error.fail",loginForm.getUsername()));
							 saveMessages(request,errors);
						 }
		     }
		 }
		 catch(NullPointerException e)
		 {
	 		 logger.error(LOGIN_ERROR + e);
	 	 }
	         return mapping.findForward(mapper);
	 }
}
